//
//  ByDocumentaryDetailVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/17.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDocumentaryDetailVC.h"
#import "ByDocumentaryDetailTitleCell.h"
#import "ByDocumentaryDetailCell.h"
#import "ByDocumentaryFourValue.h"
#import "BySuperLottoAddMinusView.h"
#import "ByDocumentaryChooseValue.h"
#import "ByFootBallBetVC.h"
#import "ByDocumentaryUserVC.h"
#import "ByBasketBallBetVC.h"

@interface ByDocumentaryDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ByNetModel *detailModel;
@property (nonatomic, strong) BySuperLottoAddMinusView *addMinusView;
@property (nonatomic, strong) ByPayBombView *payBombView;
@property (nonatomic, strong) ByNetModel *orderModel;

@end

@implementation ByDocumentaryDetailVC

- (ByPayBombView *)payBombView{
    if (_payBombView == nil) {
        _payBombView = [[ByPayBombView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        _payBombView.sureBlock = ^(){
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            [weakSelf leftItemsAction:0];
            
        };
        
        _payBombView.otherSureBlock = ^(NSString *str,NSInteger type){
            
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            
            if (type == 1) {
                ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
                vc.titleString = @"支付宝支付";
                [weakSelf by_normalPush:vc hasNavi:YES];
            }else{
                ByWechatPayQrView *vc = [[ByWechatPayQrView alloc] init];
                vc.title = @"微信支付";
                vc.str = str;
                [weakSelf by_normalPush:vc hasNavi:YES];
            }
            
        };
        _payBombView.selectBlock = ^(NSInteger index){
           
        };
    }
    return _payBombView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"跟单方案详情";
    
    
    [self loadData];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中"];
    netModel.ID = self.netModel.ID;
    [ByDocumentaryApi followOrderGodOrderDetail:netModel success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
         [self.tableView.mj_header endRefreshing];
        SLog(@"大神跟单详情 = %@", result.data);
        NSDictionary *dic = result.data[@"data"];
        self.detailModel = [[ByNetModel alloc] initContentWithDic:dic[@"orderInfo"]];
        if (self.detailModel.origin_num.length > 0) {
            if (self.detailModel.lottery_type.integerValue == 1) {
                self.detailModel.planDic = [ByFootBallHandle getFootBallGameResultWithPlan:self.detailModel.origin_num];
            }else{
                self.detailModel.planDic = [ByBasketBallHandle getBasketBallGameResultWithPlan:self.detailModel.origin_num];
            }
        }
        NSMutableArray *follow_userArray = [NSMutableArray arrayWithCapacity:1];
        for (NSDictionary *follow_userDic in dic[@"follow_user"][@"follow_user"]) {
            ByNetModel *model = [[ByNetModel alloc] initContentWithDic:follow_userDic];
            [follow_userArray addObject:model];
        }
        self.detailModel.follow_userArray = [follow_userArray copy];
        self.detailModel.amount = dic[@"follow_user"][@"amount"];
        self.detailModel.follow_num = dic[@"follow_user"][@"follow_num"];
        
        NSMutableArray *ball_infoArray = [NSMutableArray arrayWithCapacity:1];
        for (NSDictionary *ball_infoDic in dic[@"orderInfo"][@"ball_info"]) {
            ByNetModel *model = [[ByNetModel alloc] initContentWithDic:ball_infoDic];
            model.planDic = self.detailModel.planDic;
            model.public_type = self.detailModel.public_type;
            model.is_result = self.detailModel.is_result;
            if (self.detailModel.lottery_type.integerValue == 2) {
                NSDictionary *basketball_infoDic = ball_infoDic[@"basketball_info"];
                model.game_Result = [ByBasketBallHandle getDocumentaryBasketBallResultWithDic:basketball_infoDic];
            }
            [ball_infoArray addObject:model];
        }
        self.detailModel.ball_infoArray = [ball_infoArray copy];
        
        [self initViews];
        
        [self.tableView reloadData];
        
    } failure:^(ByNetBaseResult *result) {
         [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}


- (void)initViews{
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a= [dat timeIntervalSince1970];
    
    if (a < self.detailModel.end_time.integerValue && ![self.detailModel.user_id isEqualToString:BY_USER.uid]) {
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
           
            if (@available(ios 11.0,*)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-147);
            } else {
                make.top.equalTo(self.view).offset(0);
                make.bottom.equalTo(self.view).offset(-147);
            }
        }];
        
        
        UIView *bottomView = [[UIView alloc] init];
        [self.view addSubview:bottomView];
        bottomView.backgroundColor = [UIColor whiteColor];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            
            make.top.equalTo(self.tableView.mas_bottom).offset(0);
            if (@available(ios 11.0,*)) {
              make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
            } else {
               make.bottom.equalTo(self.view).offset(0);
            }
        }];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 13, 70, 14)];
        [bottomView addSubview:titleLabel];
        titleLabel.textColor = BY_TEXT_COLOR;
        titleLabel.font = FONT(14);
        titleLabel.text = @"跟单倍数";
        
        UILabel *moneyLabel = [[UILabel alloc] init];
        [bottomView addSubview:moneyLabel];
        [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(bottomView).offset(-13);
            make.centerY.equalTo(titleLabel.mas_centerY).offset(0);
            make.left.equalTo(titleLabel.mas_right).offset(10);
            make.height.mas_equalTo(@15);
        }];
        moneyLabel.textColor = BY_TEXT_COLOR;
        moneyLabel.font = FONT(14);
        moneyLabel.textAlignment = NSTextAlignmentRight;
        moneyLabel.text = [NSString stringWithFormat:@"最低跟单金额:%@元",self.detailModel.low_amount];
        
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [bottomView addSubview:sureBtn];
        [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(bottomView).offset(0);
            make.bottom.equalTo(bottomView).offset(0);
            make.height.mas_equalTo(@50);
            make.width.mas_equalTo(@100);
        }];
        sureBtn.backgroundColor = BY_THEME_COLOR;
        [sureBtn setTitle:@"投注" forState:UIControlStateNormal];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureBtn.titleLabel.font = FONT(14);
        sureBtn.tag = 100;
        [sureBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
        
        BySuperLottoAddMinusView *addMinusView = [[BySuperLottoAddMinusView alloc] init];
        [bottomView addSubview:addMinusView];
        [addMinusView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bottomView).offset(0);
            make.bottom.equalTo(bottomView).offset(0);
            make.size.mas_equalTo(CGSizeMake(150, 50));
        }];
        addMinusView.buyLabel.text = @"买";
        addMinusView.timesLabel.text = @"倍";
        addMinusView.countLabel.text = @"5";
        addMinusView.updateBlock = ^(){
            
        };
        self.addMinusView = addMinusView;
        
        UILabel *totalLabel = [[UILabel alloc] init];
        [bottomView addSubview:totalLabel];
        [totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(sureBtn.mas_left).offset(-10);
            make.left.equalTo(addMinusView.mas_right).offset(10);
            make.centerY.mas_equalTo(sureBtn.mas_centerY).offset(0);
            make.height.mas_equalTo(@16);
        }];
        totalLabel.textAlignment = NSTextAlignmentCenter;
        totalLabel.font = FONT(15);
        totalLabel.textColor = BY_THEME_COLOR;
        totalLabel.text = [NSString stringWithFormat:@"共%ld元", self.detailModel.single_amount.integerValue * 5];
        
        
        ByDocumentaryChooseValue *chooseValue = [[ByDocumentaryChooseValue alloc] init];
        [bottomView addSubview:chooseValue];
        [chooseValue mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bottomView).offset(0);
            make.right.equalTo(bottomView).offset(0);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(18);
            make.height.mas_equalTo(@30);
        }];
        chooseValue.selectValueBlock = ^(NSInteger index){
            if (index == 0) {
                self.addMinusView.countLabel.text = @"5";
            }else if (index == 1){
                self.addMinusView.countLabel.text = @"10";
            }else if (index == 2){
                self.addMinusView.countLabel.text = @"20";
            }else if (index == 3){
                self.addMinusView.countLabel.text = @"50";
            }
            totalLabel.text = [NSString stringWithFormat:@"共%ld元", self.detailModel.single_amount.integerValue * self.addMinusView.countLabel.text.integerValue];
        };
        
        
        
    }else{
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
           
            if (@available(ios 11.0,*)) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
            } else {
                make.top.equalTo(self.view).offset(0);
                make.bottom.equalTo(self.view).offset(-49);
            }
        }];
        
        UIButton *betBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:betBtn];
        [betBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            
            make.top.mas_equalTo(self.tableView.mas_bottom).offset(0);
            if (@available(ios 11.0,*)) {
               make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
            } else {
                make.bottom.equalTo(self.view).offset(0);
                
            }
        }];
        [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
        betBtn.backgroundColor = BY_THEME_COLOR;
        [betBtn setTitle:@"继续购买" forState:UIControlStateNormal];
        [betBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        betBtn.titleLabel.font = FONT(15);
        betBtn.tag = 200;
        [betBtn addTarget:self action:@selector(betAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
   
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)betAction:(UIButton *)btn{
    if (btn.tag == 100) {
        if (self.detailModel.low_amount.integerValue > self.detailModel.single_amount.integerValue * self.addMinusView.countLabel.text.integerValue) {
            [ByCoreToolAlert showOnlyText:@"跟单金额小于最低跟单金额"];
            return;
        }
        
        [ByCoreToolAlert showLoadind:@"处理中" hasMasker:YES];
        ByNetModel *netModel = [[ByNetModel alloc] init];
        netModel.plan_id = self.netModel.ID;
        netModel.num = self.addMinusView.countLabel.text;
        [ByDocumentaryApi followOrderFollowPlan:netModel success:^(ByNetBaseResult *result) {
            [ByCoreToolAlert hideHud];
            self.orderModel = [[ByNetModel alloc] initContentWithDic:result.data[@"data"]];
            self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元", self.orderModel.amount];
            self.payBombView.orderModel = self.orderModel;
            [self.payBombView show];
            
        } failure:^(ByNetBaseResult *result) {
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }else if (btn.tag == 200){
        
        if (self.netModel.lottery_type.integerValue == 1) {
            ByFootBallBetVC *vc = [[ByFootBallBetVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
        }else{
            
            ByBasketBallBetVC *vc = [[ByBasketBallBetVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
        }
        
       
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        ByDocumentaryDetailTitleCell *cell = [ByDocumentaryDetailTitleCell cellWithTableView:tableView];
        cell.netModel = self.detailModel;
        cell.tapViewBlock = ^(){
            ByDocumentaryUserVC *vc = [[ByDocumentaryUserVC alloc] init];
            self.detailModel.uid = self.detailModel.user_id;
            vc.netModel = self.detailModel;
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
    }else if (indexPath.row == 1){
        
        ByDocumentaryDetailCell *cell = [ByDocumentaryDetailCell cellWithTableView:tableView];
        cell.netModel = self.detailModel;
        return cell;
        
    }else{
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.contentView.backgroundColor = BY_BACKGROUND_COLOR;
            
        }
        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }
        
        UIView *backgroundView = [[UIView alloc] init];
        [cell.contentView addSubview:backgroundView];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.contentView).offset(5);
            make.right.equalTo(cell.contentView).offset(-5);
            make.bottom.equalTo(cell.contentView).offset(-10);
            make.top.equalTo(cell.contentView).offset(0);
        }];
        backgroundView.layer.cornerRadius = 10;
        backgroundView.backgroundColor = [UIColor whiteColor];
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [backgroundView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(10);
            make.top.equalTo(backgroundView).offset(15);
            make.right.equalTo(backgroundView).offset(-10);
            make.height.mas_equalTo(@15);
        }];
        titleLabel.font = FONT(13);
        titleLabel.textColor = [UIColor grayColor];
        titleLabel.text = [NSString stringWithFormat:@"跟单列表(%@人,%@元)",self.detailModel.follow_num,self.detailModel.amount];
        
        
        UIView *grayView = [[UIView alloc] init];
        [backgroundView addSubview:grayView];
        grayView.backgroundColor = rgba(255, 248, 249, 1);
        grayView.layer.borderColor = BY_LINE_COLOR.CGColor;
        grayView.layer.borderWidth = 1;
        
        [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(9);
            make.right.equalTo(backgroundView).offset(-9);
            make.top.equalTo(backgroundView).offset(43);
            make.bottom.equalTo(backgroundView).offset(-13);
        }];
        
        ByDocumentaryFourValue *valueView = [[ByDocumentaryFourValue alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH - 30 , 35)];
        [grayView addSubview:valueView];
        valueView.valueLabel1.text = @"用户名";
        valueView.valueLabel2.text = @"跟单金额(元)";
        valueView.valueLabel3.text = @"金额(元)";
        valueView.valueLabel4.text = @"大神佣金(元)";
        NSArray *dataArray = self.detailModel.follow_userArray;
        CGFloat top = 35;
        for (int i = 0; i < dataArray.count; i++){
            ByNetModel *netModel = [dataArray objectAtIndex:i];
            ByDocumentaryFourValue *valuesView = [[ByDocumentaryFourValue alloc] initWithFrame:CGRectMake(0, top, SCREEN_WIDTH - 30, 50)];
            [grayView addSubview:valuesView];
            top = top + 50;
            valuesView.valueLabel1.text = netModel.nickname;
            valuesView.valueLabel2.text = netModel.amount;
            valuesView.valueLabel3.text = netModel.prize_money;
            valuesView.valueLabel4.text = [NSString stringWithFormat:@"%@",netModel.commission];
            valueView.valueLabel1.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel2.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel3.textColor = [UIColor colorWithHexString:@"#666666"];
            valueView.valueLabel4.textColor = [UIColor colorWithHexString:@"#666666"];
        }
        
        
        for (int i = 0; i< dataArray.count + 1; i++) {
            UILabel *centerLine = [[UILabel alloc] init];
            [grayView addSubview:centerLine];
            centerLine.backgroundColor = BY_LINE_COLOR;
            if (i == dataArray.count) {
                [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(grayView).offset(0);
                    make.right.equalTo(grayView).offset(0);
                    make.top.equalTo(grayView).offset(35);
                    make.height.mas_equalTo(@1);
                }];
            }else{
                [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(grayView).offset(0);
                    make.right.equalTo(grayView).offset(0);
                    make.bottom.equalTo(grayView).offset(-50 * i);
                    make.height.mas_equalTo(@1);
                }];
            }
            
        }
        
        
        
        for (int i = 1; i < 4; i++) {
            UILabel *line = [[UILabel alloc] init];
            [grayView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(grayView).offset((SCREEN_WIDTH - 30) / 4.0 * i);
                make.bottom.equalTo(grayView).offset(0);
                make.top.equalTo(grayView).offset(0);
                make.width.mas_equalTo(@1);
            }];
            line.backgroundColor = BY_LINE_COLOR;
        }
        
        return cell;
        
        
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CGFloat content_height = [_detailModel.reason getRectOfStringWithSize:CGSizeMake(SCREEN_WIDTH - 34, 100) font:12].size.height + 25;
        return 50 + 75 + 25 + content_height;
    }else if (indexPath.row == 1){
        
        return 43 + 35 + Documentary_Table_Height * self.detailModel.ball_infoArray.count + 23;
        
    }else {
        
        return 43 + 35 + 50 * self.detailModel.follow_userArray.count + 23;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
